/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.vidmt.api.common.web;

import org.apache.shiro.authc.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vidmt.api.common.exceptions.BadParamException;
import com.vidmt.api.common.exceptions.CodeException;
import com.vidmt.api.common.exceptions.UserNotExistsException;
import com.vidmt.api.common.exceptions.UserNotLoginException;
import com.vidmt.api.common.pojo.ApiResultServer;

/**
 * 控制器支持类
 * 
 * @author ThinkGem
 * @version 2013-3-23
 */
public abstract class ApiController extends BaseController {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 授权登录异常
	 */
	@ExceptionHandler({ AuthenticationException.class })
	public ApiResultServer authenticationException(AuthenticationException e) {
		logger.error("error occured", e);
		return new ApiResultServer(new UserNotExistsException(e));
	}
	
	@ResponseBody
	@ExceptionHandler({ IllegalArgumentException.class })
	public ApiResultServer codeException(IllegalArgumentException e) {
		logger.error("error occured", e);
		return new ApiResultServer(new BadParamException(e));
	}
	@ResponseBody
	@ExceptionHandler({ BindException.class })
	public ApiResultServer bindException(BindException e) {
		logger.error("error occured", e);
		return new ApiResultServer(new BadParamException(new IllegalArgumentException(e)));
	}
	
	@ResponseBody
	@ExceptionHandler({ CodeException.class })
	public ApiResultServer codeException(CodeException e) {
		return new ApiResultServer(e);
	}

	@ResponseBody
	@ExceptionHandler({ Throwable.class })
	public ApiResultServer runtimeException(Throwable e) {
		logger.error("error occured", e);
		return new ApiResultServer(new CodeException(e));
	}

}
